package ro.ubbcluj.cs.validation.framework.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ro.ubbcluj.cs.validation.framework.core.ValidationError;
import ro.ubbcluj.cs.validation.framework.core.ValidationRule;
import ro.ubbcluj.cs.validation.framework.core.ValidationRuleType;
import ro.ubbcluj.cs.validation.framework.tags.utils.ResourceUtils;

public class Validator {

	private String id;
	private String paramName;
	private String[] paramValue;
	private List<ValidationRule> validationRules;

	public Validator(String id, String paramName, String[] paramValue, List<ValidationRule> validationRules) {
		super();
		this.id = id;
		this.paramName = paramName;
		this.paramValue = paramValue;
		this.validationRules = validationRules;
	}
	
	public Map<String, String> validate() {
		Map<String, String> errorMessages = new HashMap<String, String>();
		
		for(ValidationRule r : validationRules){
			if (r.getType().equals(ValidationRuleType.text)) {
				List<ValidationError> errors = new TextValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
			if (r.getType().equals(ValidationRuleType.word)) {
				List<ValidationError> errors = new WordValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
			if (r.getType().equals(ValidationRuleType.digit)) {
				List<ValidationError> errors = new DigitValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
			if (r.getType().equals(ValidationRuleType.number)) {
				List<ValidationError> errors = new NumberValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
			if (r.getType().equals(ValidationRuleType.date)) {
				List<ValidationError> errors = new DateValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
			if (r.getType().equals(ValidationRuleType.email)) {
				List<ValidationError> errors = new EmailValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
			if (r.getType().equals(ValidationRuleType.url)) {
				List<ValidationError> errors = new URLValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
			if (r.getType().equals(ValidationRuleType.file)) {
				List<ValidationError> errors = new FileValidator(paramValue, r.getParameters()).validate();
				errorMessages.putAll(getErrorMessages(errors));
			}
		}
		return errorMessages;
	}
	
	private Map<String, String> getErrorMessages(List<ValidationError> errors){
		Map<String, String> errorMessages = new HashMap<String, String>();
		ResourceUtils resourceUtils = new ResourceUtils();
		for(ValidationError e : errors){
			String validatorName = "validator:"+paramName.substring(paramName.lastIndexOf(":")+1)+":vidx"+id;
			String msg = resourceUtils.getMessage(e.getType().name());
			if (e.getMessageArgs() != null) {
				msg = resourceUtils.getMessage(e.getType().name(), e.getMessageArgs());
			}
			errorMessages.put(validatorName, msg);
		}
		return errorMessages;
	}
	
	public final String getId() {
		return id;
	}

	public final void setId(String id) {
		this.id = id;
	}

	public final String getParamName() {
		return paramName;
	}

	public final void setParamName(String paramName) {
		this.paramName = paramName;
	}

	public final String[] getParamValue() {
		return paramValue;
	}

	public final void setParamValue(String[] paramValue) {
		this.paramValue = paramValue;
	}

	public final List<ValidationRule> getValidationRules() {
		return validationRules;
	}

	public final void setValidationRules(List<ValidationRule> validationRules) {
		this.validationRules = validationRules;
	}
}
